/**
 * jQuery.websocket - jQuery websockets library /plugin
 * The objective of this plugin is to provide a easy to use, jQuery like wrapper to the functionalities offered by HTML5 web sockets.
 * @author: Bruno Félix  
 * */
 
(function($) {
  $.websocket = function(url,settings) {
  if ("WebSocket" in window) {
    //default options
    var config = {'openHandler': function(){}, //event called when the socket opens
                  'send': function(){}, //method called when a message need to be sent
                  'errorHandler': function(){}, //event called if there is an error 
                  'messageHandler': function(event){}, //event fired when a message is received
                  'closeHandler': function(){}   //event fired when the socket is closed
                  'close': function(){} //method called when the connection needs to be closed
                  };
                  
    if (settings) $.extend(config, settings);
  
    var socket = new WebSocket(url);
    socket.onopen = this.open();
    socket.onmessage = this.message(event);
    socket.onerror = this.error();
    socket.onclose = this.close();
    this.send = function(payload){return socket.send(payload);};
    this.close = function(){socket.close();};
  }else{
    //The browser does not support websockets
  }
  
  return this;
  
  };
})(jQuery);
